import Dialog from 'tdesign-miniprogram/dialog/index';
import Toast from 'tdesign-miniprogram/toast/index';
import {
  fetchCartGroupData
} from '../../services/cart/cart';

const basePage = require('../../common/BasePage')
Page({
  behaviors: [basePage],
  data: {
    cartGroupData: null,
    latitude: "",
    longitude: "",
    bgA: 0
  },

  // 调用自定义tabbar的init函数，使页面与tabbar激活状态保持一致
  onShow() {
    // this.data.latitude = getApp().globalData.latitude = res.latitude;
    // this.data.longitude = getApp().globalData.longitude = res.longitude;
    let globalData = getApp().globalData;
    if (globalData.latitude) {
      this.data.latitude = globalData.latitude;
      this.data.longitude = globalData.longitude
    }
    this.getTabBar().init();
    this.refreshData();
  },

  onLoad() {

  },
  del_click() {
    // console.log("del_click", this.data.cartGroupData);
    let sidList = [];
    this.data.cartGroupData.records.map(val => {
      val.goods.map(val1 => {
        if (val1.isSelected) {
          sidList.push(val1.sid)
        }
      })
    })
    if (sidList.length == 0) {
      return wx.showToast({
        title: '请选择要删除的商品',
        icon: 'none'
      })
    }
    wx.showModal({

      showCancel: true,
      title: '是否要删除当前选中的商品',
      success: (result) => {
        if (result.confirm) {
          this.request({
            url: this.data.host + "/v1/carts/del",
            method: "put",
            data: {
              sidList: sidList
            },
            loadingTip: '删除中...'
          }, res => {
            console.log('删除', res);
            this.req_cart()
          })
        }
      },

    })

  },
  req_cart() {
    this.request({
      url: this.data.host + "/v1/carts/me",
      data: {
        size: 50,
        latitude: this.data.latitude,
        longitude: this.data.longitude,
      },
    }, res => {
      // console.log('购物车', res);
      let totalAmount = 0;
      let cartNum = 0;
      res.data.records.map(val => {
        val.distance = (val.distance * 1).toFixed(2);
        let isSelected = true;
        val.goods.map(val1 => {
          val1.quantity = val1.number
          val1.thumb = this.get_imgUrl(val1.goodsImg);
          val1.isSelected = (val1.checked * 1 == 0)
          if (!val1.isSelected) {
            isSelected = false;
          } else {
            totalAmount += val1.price * val1.number;
          }
          cartNum += val1.number * 1
        })
        val.isSelected = isSelected
      })
      res.data.totalAmount = totalAmount;
      wx.setStorageSync('cartNum', cartNum)
      this.setData({
        cartGroupData: res.data
      })

      this.getTabBar((tabBar) => {
        tabBar.setData({
          num: cartNum
        })
      })
    })
  },
  refreshData() {
    if (this.data.latitude && this.data.longitude) {
      return this.req_cart()
    }
    wx.getLocation({
      altitude: true,
      highAccuracyExpireTime: 0,
      isHighAccuracy: true,
      type: "gcj02",

      success: (res) => {
        this.data.latitude = getApp().globalData.latitude = res.latitude;
        this.data.longitude = getApp().globalData.longitude = res.longitude;
        this.req_cart()
      },
      fail: (res) => {
        wx.showToast({
          title: '获取当前位置失败',
          icon: 'none',
        })
      },
    })

    // this.getCartGroupData().then((res) => {
    //   console.log("getCartGroupData", res);
    //   let isEmpty = true;
    //   const cartGroupData = res.data;
    //   // 一些组件中需要的字段可能接口并没有返回，或者返回的数据结构与预期不一致，需要在此先对数据做一些处理
    //   // 统计门店下加购的商品是否全选、是否存在缺货/无货
    //   for (const store of cartGroupData.storeGoods) {
    //     store.isSelected = true; // 该门店已加购商品是否全选
    //     store.storeStockShortage = false; // 该门店已加购商品是否存在库存不足
    //     if (!store.shortageGoodsList) {
    //       store.shortageGoodsList = []; // 该门店已加购商品如果库存为0需单独分组
    //     }
    //     for (const activity of store.promotionGoodsList) {
    //       activity.goodsPromotionList = activity.goodsPromotionList.filter((goods) => {
    //         goods.originPrice = undefined;

    //         // 统计是否有加购数大于库存数的商品
    //         if (goods.quantity > goods.stockQuantity) {
    //           store.storeStockShortage = true;
    //         }
    //         // 统计是否全选
    //         if (!goods.isSelected) {
    //           store.isSelected = false;
    //         }
    //         // 库存为0（无货）的商品单独分组
    //         if (goods.stockQuantity > 0) {
    //           return true;
    //         }
    //         store.shortageGoodsList.push(goods);
    //         return false;
    //       });

    //       if (activity.goodsPromotionList.length > 0) {
    //         isEmpty = false;
    //       }
    //     }
    //     if (store.shortageGoodsList.length > 0) {
    //       isEmpty = false;
    //     }
    //   }
    //   cartGroupData.invalidGoodItems = cartGroupData.invalidGoodItems.map((goods) => {
    //     goods.originPrice = undefined;
    //     return goods;
    //   });
    //   cartGroupData.isNotEmpty = !isEmpty;
    //   this.setData({
    //     cartGroupData
    //   });
    // });
  },

  // 注：实际场景时应该调用接口获取购物车数据
  getCartGroupData() {
    const {
      cartGroupData
    } = this.data;
    if (!cartGroupData) {
      return fetchCartGroupData();
    }
    return Promise.resolve({
      data: cartGroupData
    });
  },

  // 选择单个商品
  // 注：实际场景时应该调用接口更改选中状态
  selectGoodsService({
    spuId,
    skuId,
    isSelected
  }) {
    this.findGoods(spuId, skuId).currentGoods.isSelected = isSelected;
    return Promise.resolve();
  },

  // 全选门店
  // 注：实际场景时应该调用接口更改选中状态
  selectStoreService({
    storeId,
    isSelected
  }) {
    const currentStore = this.data.cartGroupData.storeGoods.find((s) => s.storeId === storeId);
    currentStore.isSelected = isSelected;
    currentStore.promotionGoodsList.forEach((activity) => {
      activity.goodsPromotionList.forEach((goods) => {
        goods.isSelected = isSelected;
      });
    });
    return Promise.resolve();
  },

  // 加购数量变更
  // 注：实际场景时应该调用接口
  changeQuantityService({
    spuId,
    skuId,
    quantity
  }) {
    this.findGoods(spuId, skuId).currentGoods.quantity = quantity;
    return Promise.resolve();
  },

  // 删除加购商品
  // 注：实际场景时应该调用接口
  deleteGoodsService() {
    function deleteGoods(group) {
      for (const gindex in group) {
        const goods = group[gindex];
        if (goods.spuId === spuId && goods.skuId === skuId) {
          group.splice(gindex, 1);
          return gindex;
        }
      }
      return -1;
    }
    const {
      storeGoods,
      invalidGoodItems
    } = this.data.cartGroupData;
    for (const store of storeGoods) {
      for (const activity of store.promotionGoodsList) {
        if (deleteGoods(activity.goodsPromotionList) > -1) {
          return Promise.resolve();
        }
      }
      if (deleteGoods(store.shortageGoodsList) > -1) {
        return Promise.resolve();
      }
    }
    if (deleteGoods(invalidGoodItems) > -1) {
      return Promise.resolve();
    }
    return Promise.reject();
  },

  // 清空失效商品
  // 注：实际场景时应该调用接口
  clearInvalidGoodsService() {
    this.data.cartGroupData.invalidGoodItems = [];
    return Promise.resolve();
  },

  onGoodsSelect(e) {
    console.log('onGoodsSelect', e);
    let sidList = [];
    if (e.detail.goods) {
      sidList.push(e.detail.goods.sid)
    }
    let checked = e.detail.isSelected ? 0 : 1
    this.request({
      url: this.data.host + '/v1/carts/checked',
      method: "post",
      data: {
        checked: checked,
        sidList: sidList
      }
    }, res => {
      this.refreshData();
    })
  },

  onStoreSelect(e) {
    console.log('onStoreSelect', e);
    let sidList = [];
    if (e.detail.store.goods) {
      e.detail.store.goods.map(val => {
        sidList.push(val.sid)
      })

    }
    let checked = e.detail.isSelected ? 0 : 1
    this.request({
      url: this.data.host + '/v1/carts/checked',
      method: "post",
      data: {
        checked: checked,
        sidList: sidList
      }
    }, res => {
      this.refreshData();
    })
    // const {
    //   store: {
    //     storeId
    //   },
    //   isSelected,
    // } = e.detail;
    // this.selectStoreService({
    //   storeId,
    //   isSelected
    // }).then(() => this.refreshData());
  },

  onQuantityChange(e) {
    console.log('onQuantityChange', e);
    let goods = e.detail.goods;
    let quantity = e.detail.quantity;
    if (quantity > goods.stock) {
      return wx.showToast({
        title: '库存不足',
        icon: 'none',
      })
    }
    //
    this.request({
      url: this.data.host + "/v1/carts/number",
      method: "put",
      data: {
        number: quantity,
        sid: goods.sid
      }
    }, res => {
      console.log('修改', res);
      this.req_cart()
    })
    // return;
    // const {
    //   goods: {
    //     spuId,
    //     skuId
    //   },
    //   quantity,
    // } = e.detail;
    // const {
    //   currentGoods
    // } = this.findGoods(spuId, skuId);
    // let currentGoods = this.
    // const stockQuantity = currentGoods.stockQuantity > 0 ? currentGoods.stockQuantity : 0; // 避免后端返回的是-1
    // // 加购数量超过库存数量
    // if (quantity > stockQuantity) {
    //   // 加购数量等于库存数量的情况下继续加购
    //   if (currentGoods.quantity === stockQuantity && quantity - stockQuantity === 1) {
    //     Toast({
    //       context: this,
    //       selector: '#t-toast',
    //       message: '当前商品库存不足',
    //     });
    //     return;
    //   }
    //   Dialog.confirm({
    //       title: '商品库存不足',
    //       content: `当前商品库存不足，最大可购买数量为${stockQuantity}件`,
    //       confirmBtn: '修改为最大可购买数量',
    //       cancelBtn: '取消',
    //     })
    //     .then(() => {
    //       this.changeQuantityService({
    //         spuId,
    //         skuId,
    //         quantity: stockQuantity,
    //       }).then(() => this.refreshData());
    //     })
    //     .catch(() => {});
    //   return;
    // }
    // this.changeQuantityService({
    //   spuId,
    //   skuId,
    //   quantity
    // }).then(() => this.refreshData());
  },

  goCollect() {
    /** 活动肯定有一个活动ID，用来获取活动banner，活动商品列表等 */
    const promotionID = '123';
    wx.navigateTo({
      url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
    });
  },
  onPageScroll: function (ev) {
    // console.log('onPageScroll', ev);
    this.setData({
      bgA: ev.scrollTop / 200
    })
  },


  goGoodsDetail(e) {
    const {
      spuId,
      storeId
    } = e.detail.goods;
    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${spuId}&storeId=${storeId}`,
    });
  },

  clearInvalidGoods() {
    // 实际场景时应该调用接口清空失效商品
    this.clearInvalidGoodsService().then(() => this.refreshData());
  },

  onGoodsDelete(e) {
    // return console.log("onGoodsDelete", e);

    Dialog.confirm({
      content: '确认删除该商品吗?',
      confirmBtn: '确定',
      cancelBtn: '取消',
    }).then(() => {
      this.request({
        url: this.data.host + "/v1/carts/del",
        method: "put",
        data: {
          sidList: [e.detail.goods.sid]
        }
      }, res => {
        console.log('删除', res);
        this.req_cart()
      })
      // this.deleteGoodsService({
      //   spuId,
      //   skuId
      // }).then(() => {
      //   Toast({
      //     context: this,
      //     selector: '#t-toast',
      //     message: '商品删除成功'
      //   });
      //   this.refreshData();
      // });
    });
  },

  onSelectAll(event) {
    const {
      isAllSelected
    } = event?.detail ?? {};
    Toast({
      context: this,
      selector: '#t-toast',
      message: `${isAllSelected ? '取消' : '点击'}了全选按钮`,
    });
    // 调用接口改变全选
  },

  onToSettle() {
    const goodsRequestList = [];
    // this.data.cartGroupData.storeGoods.forEach((store) => {
    //   store.promotionGoodsList.forEach((promotion) => {
    //     promotion.goodsPromotionList.forEach((m) => {
    //       if (m.isSelected == 1) {
    //         goodsRequestList.push(m);
    //       }
    //     });
    //   });
    // });
    this.request({
      url: this.data.host + "/v1/carts/preorder",
      method: "post"
    }, res => {
      console.log('preorder', res);
      // wx.setStorageSync('order.goodsRequestList', JSON.stringify(goodsRequestList));
      wx.navigateTo({
        url: '/pages/order/list'
      });
    })

  },
  onGotoHome() {
    wx.switchTab({
      url: '/pages/home/home'
    });
  },
});